;layui.define(function (e) {
    layui.use(['user', 'jquery', 'form'], function () {
        var setter = layui.setter
            , form = layui.form
            , $ = layui.jquery;
        var b = $("input");
        var a = $("body");
        var index;
        b.on('keypress', function (e) {
            if (e.which == 13) {
                $("#userLoginBtn").click();
            }
        });
        a.on("click", "#userGetVercode", function () {
            $(this);
            this.src = "/captcha?t=" + (new Date).getTime()
        });
        //登录按钮
        form.on("submit(login)", function (data) {
            var _that = $(this);
            _that.text("登录中...").attr("disabled", "disabled").addClass("layui-disabled");
            index = top.layer.msg('登录中...请稍候', {icon: 16, time: false, shade: 0.8});
            $.ajax({
                type: 'post',
                dataType: 'json',
                data: data.field || {},
                url: '/login',
                success: function (res) {
                    console.log(res);
                    top.layer.close(index);
                    if (res.code === 0) {
                        //请求成功后，写入 access_token
                        layui.data(setter.tableName, {
                            key: setter.request.tokenName
                            , value: JSON.stringify(res.data)
                        });

                        //登入成功的提示与跳转
                        layer.msg('登入成功', {
                            offset: '15px'
                            , icon: 1
                            , time: 1000
                        }, function () {
                            location.href = res.result;
                        });
                    }else{
                        //登入成功的提示与跳转
                        layer.msg(res.msg, {
                            offset: '15px'
                            , icon: 2
                            , time: 1000
                        },function () {
                            $('#userGetVercode').click();
                            _that.text("登录").removeAttr("disabled").removeClass("layui-disabled");
                        });
                    }
                },
                error: function (e) {
                    top.layer.close(index);
                    $('#userGetVercode').click();
                    _that.text("登录").removeAttr("disabled").removeClass("layui-disabled");
                }
            });
            return false;
        });
        //登录按钮
        form.on("submit(resetPwd)", function (data) {
            var _that = $(this);
            _that.text("重置密码中...").attr("disabled", "disabled").addClass("layui-disabled");
            index = top.layer.msg('重置中...请稍候', {icon: 16, time: false, shade: 0.8});
            $.ajax({
                type: 'post',
                dataType: 'json',
                data: data.field || {},
                url: '/core/user/resetPwd',
                success: function (res) {
                    top.layer.close(index);
                    layer.msg('重置密码成功', {
                        offset: '15px'
                        , icon: 1
                        , time: 1000
                    }, function () {
                        location.href = res.result;
                    });
                },
                error: function (e) {
                    top.layer.close(index);
                    _that.text("重置密码").removeAttr("disabled").removeClass("layui-disabled");
                }
            });
            return false;
        });
    }), e("user", {})
});